package com.zjb.wxlisener.control;

import com.google.gson.JsonObject;
import com.zjb.wxlisener.JavaBean.LoginInfo;
import com.zjb.wxlisener.JavaBean.PCuser;
import com.zjb.wxlisener.JavaBean.Result;
import com.zjb.wxlisener.JavaBean.User;
import com.zjb.wxlisener.service.LoginService;
import com.zjb.wxlisener.until.JWTUntil;
import com.zjb.wxlisener.until.QiNiuUtil;
import com.zjb.wxlisener.until.WechatUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.sound.sampled.Line;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/login")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class LoginControl {
    @Autowired
    LoginService loginService;

    @PostMapping("/pc")
    public Result PClogin(@RequestBody PCuser pCuser){
        try {
            HashMap<String,Object> hashMap = new HashMap<>();
            PCuser user = loginService.PClogin(pCuser);
            hashMap.put("password",pCuser.getPassword());
            hashMap.put("phonenumber",pCuser.getPhonenumber());
            String jwt = JWTUntil.getJWT(hashMap);
            System.out.println(user);
            if (user==null){
                return Result.error("没有该用户");
            }
            List<Object> userlist = null;
            userlist.add(user);
            userlist.add(jwt);
            return Result.success(userlist);
        }catch (Exception e){
            return Result.error("添加失败");
        }

    }
    @GetMapping("/adduser")
    public Result addUserinfo(User user ) throws IOException {
        try {
            HashMap<String,Object> hashMap = new HashMap<>();
            JsonObject jsonObject = WechatUtil.getOpenId(user.getCode());
            user.setOpenid(jsonObject.get("openid").getAsString());
            loginService.addUser(user);
            hashMap.put("username",user.getUsername());
            hashMap.put("phonenumber",user.getPhonenumber());
            hashMap.put("openid",user.getOpenid());
            String jwt = JWTUntil.getJWT(hashMap);
            return Result.success(new LoginInfo(user,jwt));
        }catch (Exception e){
            return Result.error("添加失败");
        }
    }
    @PostMapping("/imgUrl")
    public Result addimgUrl(@RequestParam("file") MultipartFile imgUrl,@RequestParam("openid") String openid) throws Exception {
        String url = loginService.addimgUrl(imgUrl,openid);
        if (url==null){
            return Result.error("头像上传失败");
        }
        return Result.success(url);
    }
    @GetMapping("/check")
    public Result checkUser(String code){
        User user = loginService.checkUser(code);
        if(user==null){
            return Result.error("用户不存在");
        }
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("username",user.getUsername());
        hashMap.put("phonenumber",user.getPhonenumber());
        hashMap.put("openid",user.getOpenid());
        String jwt = JWTUntil.getJWT(hashMap);
        return Result.success(new LoginInfo(user,jwt));
    }
}
